package drone.basis.util ;

import java.util.Random ;
import java.util.zip.CRC32 ;

public class ColorUtil
{

	public static void main (
			String [ ] args
	)
	{
		String d = buildColor ( "43" ) ;
		System.out.println ( d ) ;
	}

	public static String buildColor (
			String source
	)
	{

		if ( StringUtil.isEmpty ( source ) )
		{
			return "#000000" ;
		}

		String redBase = DigestUtil.md5 ( source ) ;
		String greenBase = DigestUtil.md5 ( redBase ) ;
		String blueBase = DigestUtil.md5 ( greenBase ) ;

		CRC32 crc32 = new CRC32 ( ) ;
		crc32.update ( redBase.getBytes ( ) ) ;
		String red = String.valueOf ( crc32.getValue ( ) % 256 ) ;

		crc32.update ( greenBase.getBytes ( ) ) ;
		String green = String.valueOf ( crc32.getValue ( ) % 256 ) ;

		crc32.update ( blueBase.getBytes ( ) ) ;
		String blue = String.valueOf ( crc32.getValue ( ) % 256 ) ;

		red = Integer.toHexString ( Integer.valueOf ( red ) ).toUpperCase ( ) ;
		green = Integer.toHexString ( Integer.valueOf ( green ) ).toUpperCase ( ) ;
		blue = Integer.toHexString ( Integer.valueOf ( blue ) ).toUpperCase ( ) ;

		red = red.length ( ) == 1 ? "0" + red : red ;
		green = green.length ( ) == 1 ? "0" + green : green ;
		blue = blue.length ( ) == 1 ? "0" + blue : blue ;

		return "#" + red + green + blue ;

	}

	public static String randomColor ( )
	{
		String red ;
		// 绿色
		String green ;
		// 蓝色
		String blue ;
		// 生成随机对象
		Random random = new Random ( ) ;
		// 生成红色颜色代码
		red = Integer.toHexString ( random.nextInt ( 256 ) ).toUpperCase ( ) ;
		// 生成绿色颜色代码
		green = Integer.toHexString ( random.nextInt ( 256 ) ).toUpperCase ( ) ;
		// 生成蓝色颜色代码
		blue = Integer.toHexString ( random.nextInt ( 256 ) ).toUpperCase ( ) ;

		red = red.length ( ) == 1 ? "0" + red : red ;
		green = green.length ( ) == 1 ? "0" + green : green ;
		blue = blue.length ( ) == 1 ? "0" + blue : blue ;
		
		return "#" + red + green + blue ;

	}
}
